package com.webull.information.center.carwler.dao;

import com.webull.framework.util.UtilJdbc;
import com.webull.information.center.carwler.common.model.CarwlerJobConfig;
import com.webull.information.center.dao.JdbcHelper;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 *
 */
@Repository
public class CarwlerJobConfigDao {
    private static final RowMapper<CarwlerJobConfig> jobConfig = BeanPropertyRowMapper.newInstance(CarwlerJobConfig.class);
    protected final Logger logger = LogManager.getLogger(getClass());
    private @Autowired
    JdbcTemplate jdbcTemplate;
    private @Autowired
    NamedParameterJdbcTemplate npjdbcTemplate;

    public CarwlerJobConfig searchByCarwler(String carwler) {
        logger.trace("method={},sql={}, Carwler={}", "CarwlerJobConfig", Sql.searchByCarwler, carwler);
        return UtilJdbc.queryForObject(jdbcTemplate, Sql.searchByCarwler, jobConfig, carwler);
    }

    public Integer getPastNewsIdByCarwler(String carwler) {
        logger.trace("method={},sql={}, Carwler={}", "getPastNewsIdByCarwler", Sql.searchNewsIdByCarwler, carwler);
        return UtilJdbc.queryForObject(jdbcTemplate, Sql.searchNewsIdByCarwler, Integer.class, carwler);
    }

    public Integer getPastBaRelIdByCarwler(String carwler) {
        logger.trace("method={},sql={}, Carwler={}", "getPastNewsIdByCarwler", Sql.searchNewsIdByCarwler, carwler);
        return UtilJdbc.queryForObject(jdbcTemplate, Sql.searchBaseRelIdByCarwler, Integer.class, carwler);
    }

    public List<CarwlerJobConfig> searchBranchCarwler() {
        logger.trace("method={},sql={}, Carwler={}", "CarwlerJobConfig", Sql.searchByCarwler);
        return jdbcTemplate.query(Sql.searchBranchCarwler, jobConfig);
    }


    public void update(CarwlerJobConfig config) {
        npjdbcTemplate.update(Sql.update, JdbcHelper.getSqlParameterSource(config));
    }

    private class Sql {
        private static final String searchBranchCarwler = "select * from wli_carwler_job_config where del=0 and base = 0";
        private static final String searchByCarwler = "select * from wli_carwler_job_config where del=0 and carwler = ?";
        private static final String searchNewsIdByCarwler = "select past_news_id from wli_carwler_job_config where del=0 and carwler = ?";
        private static final String searchBaseRelIdByCarwler = "select past_ba_rel_id from wli_carwler_job_config where del=0 and carwler = ?";

        private static final String update = "update wli_carwler_job_config set past_news_id = ifnull(:pastNewsId,past_news_id),past_ba_rel_id=ifnull(:pastBaRelId,past_ba_rel_id)  where  id = :id";

    }


//    private @Autowired
//    CarwlerJobStateUtil carwlerJobStateUtil;
//
//    @Scheduled(cron = "0 0/1 * * * ?")
//    public void delGarbageData() {
//        if (carwlerJobStateUtil.isCarwlerBase()) {
//            logger.info("carwlerJobStateUtil_delGarbageData start:");
//            while0:
//            while (true) {
//                List<Integer> integers = jdbcTemplate.queryForList("select id from DELETE_id where state =0 limit 100", Integer.class);
//                if (integers == null || integers.isEmpty()) break while0;
//                for (Integer newsId : integers) {
//                    jdbcTemplate.update("DELETE from  wli_news_ticker_rel_2 where news_id = ?", newsId);
//                    jdbcTemplate.update("DELETE from  wli_news_future_cat_rel_2  where news_id = ?", newsId);
//                    jdbcTemplate.update("DELETE from  wli_news_2  where id = ?", newsId);
//                    jdbcTemplate.update("update DELETE_id set state = 1 where id = ?", newsId);
//                }
//                logger.info("carwlerJobStateUtil_delGarbageData mid:{},maxId,", integers.size(), integers.get(integers.size() - 1));
//            }
//
//        }
//    }

}
